时间同步完成,待联调

chengzhenyu 8 years ago
parent
commit
4c71a6500b

+ 49 - 23
app/src/main/java/ai/pai/lensman/main/MainPresenter.java

@@ -8,8 +8,8 @@ import android.content.IntentFilter;
8 8
 import android.content.ServiceConnection;
9 9
 import android.net.ConnectivityManager;
10 10
 import android.os.IBinder;
11
+import android.support.annotation.StringRes;
11 12
 
12
-import com.android.common.utils.LogHelper;
13 13
 import com.android.common.utils.NetworkUtil;
14 14
 
15 15
 import java.text.SimpleDateFormat;
@@ -27,7 +27,7 @@ import ai.pai.lensman.db.Preferences;
27 27
 import ai.pai.lensman.service.OrderDealService;
28 28
 import ai.pai.lensman.service.UploadService;
29 29
 
30
-class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorListener<ArrayList<String>>,
30
+class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorListener<ArrayList<String>>,SyncTimeInteractor.SyncTimeListener,
31 31
         UploadService.PhotoUploadListener, QueryBoxStatusInteractor.BoxStatusListener,OrderDealService.OrderServiceListener {
32 32
 
33 33
     private int sessionSeq;
@@ -37,13 +37,15 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL
37 37
     private ArrayList<SessionBean> sessionList;
38 38
 
39 39
     private MainContract.View view;
40
-    private FetchSessionIdsInteractor interactor;
40
+    private FetchSessionIdsInteractor fetchSessionIdsInteractor;
41 41
     private QueryBoxStatusInteractor boxStatusInteractor;
42
+    private SyncTimeInteractor syncTimeInteractor;
42 43
 
43 44
     private ServiceConnection uploadServiceConnection;
44 45
     private ServiceConnection orderServiceConnection;
45 46
 
46 47
     private boolean isBoxConnected = true;
48
+    private boolean isTimeSync = false;
47 49
 
48 50
 
49 51
     private BroadcastReceiver wifiReceiver  = new BroadcastReceiver() {
@@ -64,8 +66,9 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL
64 66
         this.view = view;
65 67
         this.lensmanId = Preferences.getInstance().getLensManId();
66 68
         sessionIds = new ArrayList<>();
67
-        interactor = new FetchSessionIdsInteractor(lensmanId,100,this);
69
+        fetchSessionIdsInteractor = new FetchSessionIdsInteractor(lensmanId,100,this);
68 70
         boxStatusInteractor = new QueryBoxStatusInteractor(this);
71
+        syncTimeInteractor = new SyncTimeInteractor(lensmanId,this);
69 72
         uploadServiceConnection = new ServiceConnection() {
70 73
             @Override
71 74
             public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
@@ -99,23 +102,41 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL
99 102
         }else{
100 103
             view.showBoxDisconnectedView();
101 104
         }
102
-
105
+        view.setNewSessionBtnEnabled(false);
103 106
         if(sessionIds==null|| sessionIds.size()<20){
104
-            interactor.startJob();
107
+            fetchSessionIdsInteractor.startJob();
108
+        }
109
+        if(!isTimeSync){
110
+            syncTimeInteractor.startJob();
105 111
         }
112
+        boxStatusInteractor.startJob();
113
+
106 114
         if(BuildConfig.isTestMode){
107 115
             if(sessionIds==null || sessionIds.size()==0){
108 116
                 sessionIds = new ArrayList<>();
109 117
                 sessionIds.add(String.valueOf(System.currentTimeMillis()));
110 118
             }
111 119
         }
112
-        if(sessionIds==null || sessionIds.size()<1){
113
-            view.setNewSessionBtnEnabled(false);
114
-            view.showSnackBar(R.string.prepare_in_processing);
115
-        }else{
116
-            view.setNewSessionBtnEnabled(true);
117
-        }
120
+        registerWifiChangeReceiver();
121
+        App.getAppContext().startService(new Intent(App.getAppContext(), OrderDealService.class));
122
+        App.getAppContext().bindService(new Intent(App.getAppContext(),OrderDealService.class),
123
+                orderServiceConnection, Context.BIND_AUTO_CREATE);
124
+        App.getAppContext().startService(new Intent(App.getAppContext(), UploadService.class));
125
+        App.getAppContext().bindService(new Intent(App.getAppContext(),UploadService.class),
126
+                uploadServiceConnection, Context.BIND_AUTO_CREATE);
127
+
118 128
         sessionList = DBService.getInstance().getSessionBeanListByDay(getSessionDateInLongFormat());
129
+        onDataReady();
130
+    }
131
+
132
+    private void onDataReady(){
133
+        if(!isTimeSync){
134
+            return;
135
+        }
136
+        if(sessionIds==null|sessionIds.size()<20){
137
+            return;
138
+        }
139
+        view.setNewSessionBtnEnabled(true);
119 140
         if(sessionList.size()==0){
120 141
             view.showEmptyView();
121 142
         }else{
@@ -123,19 +144,13 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL
123 144
             view.refreshSessionViews(sessionList);
124 145
         }
125 146
         sessionSeq = sessionList.size();
126
-        registerWifiChangeReceiver();
127
-        boxStatusInteractor.startJob();
128
-        App.getAppContext().startService(new Intent(App.getAppContext(), OrderDealService.class));
129
-        App.getAppContext().bindService(new Intent(App.getAppContext(),OrderDealService.class),
130
-                orderServiceConnection, Context.BIND_AUTO_CREATE);
131
-        App.getAppContext().startService(new Intent(App.getAppContext(), UploadService.class));
132
-        App.getAppContext().bindService(new Intent(App.getAppContext(),UploadService.class),
133
-                uploadServiceConnection, Context.BIND_AUTO_CREATE);
134 147
     }
135 148
 
136 149
     @Override
137 150
     public void stop() {
138 151
         boxStatusInteractor.cancelJob();
152
+        syncTimeInteractor.cancelJob();
153
+        fetchSessionIdsInteractor.cancelJob();
139 154
         App.getAppContext().unbindService(uploadServiceConnection);
140 155
         App.getAppContext().unbindService(orderServiceConnection);
141 156
         App.getAppContext().unregisterReceiver(wifiReceiver);
@@ -163,14 +178,13 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL
163 178
     @Override
164 179
     public void onInteractSuccess(ArrayList<String> sessionIds) {
165 180
         this.sessionIds.addAll(sessionIds);
166
-        view.setNewSessionBtnEnabled(true);
167
-        view.showBoxConnectedView();
181
+        onDataReady();
168 182
     }
169 183
 
170 184
     @Override
171 185
     public void onInteractFail(String errorMsg) {
172 186
         if(NetworkUtil.isWifiConnected(App.getAppContext())){
173
-            interactor.startJob();
187
+            fetchSessionIdsInteractor.startJob();
174 188
         }else{
175 189
             view.showBoxDisconnectedView();
176 190
         }
@@ -252,4 +266,16 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL
252 266
     public void onOrderCompleted() {
253 267
 
254 268
     }
269
+
270
+    @Override
271
+    public void onTimeSync(boolean result, @StringRes int strId) {
272
+        isTimeSync = result;
273
+        if(result){
274
+            onDataReady();
275
+        }else{
276
+            if(strId!=0){
277
+                view.showSnackBar(strId);
278
+            }
279
+        }
280
+    }
255 281
 }

+ 122 - 0
app/src/main/java/ai/pai/lensman/main/SyncTimeInteractor.java

@@ -0,0 +1,122 @@
1
+package ai.pai.lensman.main;
2
+
3
+import android.os.AsyncTask;
4
+import android.os.SystemClock;
5
+import android.support.annotation.StringRes;
6
+
7
+import com.android.common.executors.ThreadExecutor;
8
+import com.android.common.http.HttpUtils;
9
+
10
+import org.json.JSONObject;
11
+
12
+import java.util.HashMap;
13
+
14
+import ai.pai.lensman.BuildConfig;
15
+import ai.pai.lensman.R;
16
+import ai.pai.lensman.base.BaseInteractor;
17
+import ai.pai.lensman.utils.BoxUrlContainer;
18
+import ai.pai.lensman.utils.HttpPostTask;
19
+import ai.pai.lensman.utils.UrlContainer;
20
+
21
+/**
22
+ * Created by chengzhenyu on 2017/4/4.
23
+ */
24
+
25
+public class SyncTimeInteractor implements BaseInteractor {
26
+    private HttpPostTask timeSyncTask;
27
+    private String lensmanId;
28
+    private SyncTimeListener listener;
29
+    private boolean isCancelled;
30
+
31
+    public SyncTimeInteractor(String lensmanId,  SyncTimeListener listener){
32
+        this.listener = listener;
33
+        this.lensmanId = lensmanId;
34
+    }
35
+
36
+    @Override
37
+    public void startJob() {
38
+        cancelJob();
39
+        isCancelled = false;
40
+        HashMap<String,String> params = new HashMap<>();
41
+        params.put("user_id",lensmanId);
42
+        if(BuildConfig.isTestMode){
43
+            listener.onTimeSync(true,0);
44
+        }
45
+        timeSyncTask = new HttpPostTask(params){
46
+
47
+            int msgId = R.string.sync_time_box_error;
48
+
49
+            @Override
50
+            protected boolean parseResponse(String response) {
51
+                try{
52
+                    JSONObject json = new JSONObject(response);
53
+                    int status = json.getInt("status");
54
+                    if(status == 200){
55
+                        JSONObject data = json.getJSONObject("data");
56
+                        String time = data.getString("time");
57
+                        HashMap<String,String> boxParams = new HashMap<>();
58
+                        boxParams.put("time",time);
59
+                        if(isCancelled){
60
+                            return false;
61
+                        }
62
+                        HttpUtils.doHttpPost(BoxUrlContainer.SET_TIME_URL,boxParams);
63
+                        if(isCancelled){
64
+                            return false;
65
+                        }
66
+                        SystemClock.sleep(1000);
67
+                        if(isCancelled){
68
+                            return false;
69
+                        }
70
+                        String boxResp = HttpUtils.doHttpPost(BoxUrlContainer.BOX_TIME_URL,null);
71
+                        if(isCancelled){
72
+                            return false;
73
+                        }
74
+                        JSONObject boxJson = new JSONObject(boxResp);
75
+                        int boxStatus = boxJson.getInt("status");
76
+                        if(boxStatus == 200){
77
+                            String boxTime = boxJson.getJSONObject("data").getString("time");
78
+                            if(time.substring(0,13).equals(boxTime.substring(0,13))){
79
+                                return true;
80
+                            }
81
+                        }
82
+                    }else{
83
+                        msgId = R.string.sync_time_server_error;
84
+                    }
85
+                }catch (Exception e){
86
+                    e.printStackTrace();
87
+                }
88
+                return false;
89
+            }
90
+
91
+            @Override
92
+            protected void onPostFail() {
93
+                super.onPostFail();
94
+                listener.onTimeSync(false,msgId);
95
+            }
96
+
97
+            @Override
98
+            protected void onPostSuccess() {
99
+                super.onPostSuccess();
100
+                listener.onTimeSync(true,0);
101
+            }
102
+        };
103
+        timeSyncTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.SERVER_TIME_URL);
104
+    }
105
+
106
+    @Override
107
+    public void cancelJob() {
108
+        if(timeSyncTask==null){
109
+            return;
110
+        }
111
+        if(timeSyncTask.getStatus()== AsyncTask.Status.RUNNING){
112
+            timeSyncTask.cancel(true);
113
+        }
114
+        isCancelled = true;
115
+        timeSyncTask = null;
116
+    }
117
+
118
+    public interface SyncTimeListener{
119
+
120
+        void onTimeSync(boolean result,@StringRes int strId);
121
+    }
122
+}

+ 4 - 0
app/src/main/res/values/strings.xml

@@ -173,4 +173,8 @@
173 173
     <string name="upload_fast">短</string>
174 174
 
175 175
     <string name="upload_settings">上传管理</string>
176
+
177
+    <string name="sync_time_server_error">时间同步出错,请检查网络连接后退出重试</string>
178
+    <string name="sync_time_box_error">时间同步出错,请检查盒子是否打开后退出重试</string>
179
+
176 180
 </resources>